using System.Collections;
using UnityEngine;

public class PlayerHurtObj : MonoBehaviour
{
    [Header("受攻击")]
    public GameObject hurtObj;
    [Header("动画")]
    public Animator ani;
    [Header("正在攻击的敌人")]
    public Enemy currAttackEnemy;
    [Header("剑特效")]
    public GameObject finalSwordEff;

    //骨棒计数
    public int skeletonNum = 0;
    //连击矛计数
    public int moreSpearNum = 0;

    private void Start()
    {
        //获取组件
        ani = hurtObj.GetComponent<Animator>();
    }
    //设置受攻击的位置
    public void Attack(Vector2 pos,float scale,int damage)
    {
        hurtObj.GetComponent<SpriteRenderer>().enabled = true;
        hurtObj.transform.position = pos;
        //受到大小影响
        hurtObj.transform.localScale = new Vector3(scale, scale, scale);
        //播放
        ani.SetTrigger("Attack");
    }
    //协程开启攻击模式
    public IEnumerator IAttack(Player player,Enemy enemy)
    {
        //无法进行操作
        GameManager.Instance.canOprate = false;
        //进入战斗
        EnterBattle(player, enemy);
        //定义一个flag，1攻击玩家，2攻击敌人
        int flag = 2;
        //查看敌人类型
        if (enemy.data.type==EnemyType.Quick) flag=1;
        else if (enemy.data.type==EnemyType.Slow) flag=2;
        else if(enemy.data.type==EnemyType.Routine)
        {
            if(enemy.data.damage>player.damage)
            {
                flag=1;
            }
            else
            {
                flag=2;
            }
        }
        //如果拥有速血刀
        if(ArtifactManager.instance.SeekArtifact("速刃"))
        {
            flag=2;
        }
        //如果拥有骨棒，且为后攻击
        if(ArtifactManager.instance.SeekArtifact("骨棒"))
        {
            if(player.transform.position.y>enemy.transform.position.y)
            {
                flag = 2;
            }
        }
        //循环进行攻击
        while (player.currHp>0&&enemy.data.currHp>0)
        {
            if (flag==1)
            {
                AttackPlayer(player, enemy);
            }
            else
            {
                AttackEnemy(player, enemy);
            }
            if (player.currHp <= 0 || enemy.data.currHp <= 0) break;
            if(flag==1)
            {
                yield return new WaitForSeconds(0.3f*(enemy.data.damageNum-1)+0.6f);
            }
            else
            {
                yield return new WaitForSeconds(0.6f);
            }

            //查看此时flag
            flag = flag== 1 ? 2 : 1;
        }
        //查看哪个血量少于
        if (player.currHp<=0)
        {
            //调用玩家死亡
            player.Die();
        }
        else if(enemy.data.currHp<=0)
        {
            //敌人可行走
            enemy.SetPosRun();
            //可以进行操作
            GameManager.Instance.canOprate = true;
        }
        //退出战斗
        ExitBattle(player, enemy);
    }
    //攻击玩家
    public void AttackPlayer(Player player, Enemy enemy)
    {
        StartCoroutine(playerMoreHurt(player, enemy));
    }
    //协程玩家受伤害
    public IEnumerator playerMoreHurt(Player player, Enemy enemy)
    {
        //获取敌人攻击数量
        for(int i=0;i<enemy.data.damageNum;i++)
        {
            BeHurtPlayer(player, enemy);

            yield return new WaitForSeconds(0.3f);
        }
    }
    //玩家受伤害
    public void BeHurtPlayer(Player player, Enemy enemy)
    {
        //移动到玩家位置
        Attack(player.transform.position, 1, enemy.data.damage);
        int beDamage = enemy.data.damage;
        //如果拥有鼠符咒
        if (TurnManager.instance.SeekTurn("猪"))
        {
            //60概率50免伤，且敌人受到所有伤害
            if (Random.Range(0, 100)<60)
            {
                beDamage/=2;
                beDamage=beDamage<1 ? 1 : beDamage;
                //敌人受伤害
                enemy.BeHurt(enemy.data.damage, false);
            }
        }
        //玩家受伤
        player.BeHurt(Mathf.Abs(beDamage));
    }
    //攻击敌人
    public void AttackEnemy(Player player,Enemy enemy)
    {
        //如果拥有均扇
        if (ArtifactManager.instance.SeekArtifact("均扇"))
        {
            //给其他敌人造成伤害
            SceneItemManager.instance.OtherEnemyBeHurt(Mathf.RoundToInt(player.GetPlayerDamage()/2.0f),enemy);
        }
        int currDamage = player.CalculateFinalDamage(player.GetPlayerDamage(), enemy);
        //如果拥有骨棒
        if (ArtifactManager.instance.SeekArtifact("骨棒"))
        {
            if (skeletonNum<=0)
            {
                if(player.transform.position.y-enemy.transform.position.y>0.5f)
                {
                    skeletonNum+=1;
                    currDamage+=Mathf.RoundToInt(currDamage*0.6f);
                }
            }
        }
        //如果拥有连击矛
        if(ArtifactManager.instance.SeekArtifact("连击矛"))
        {
            currDamage += Mathf.RoundToInt(currDamage * moreSpearNum*0.5f);
            moreSpearNum += 1;
            if (moreSpearNum >= 4) moreSpearNum = 4;
        }
        //是否拥有血之矛
        if (GameManager.Instance.bloodNum%2==0)
        {
            if (ArtifactManager.instance.SeekArtifact("血之矛"))
            {
                player.AddHp(Mathf.RoundToInt(currDamage * 0.8f));
            }
        }
        //移动到敌人位置
        Attack(enemy.transform.position, enemy.data.scaleSize,currDamage);
        //敌人受伤
        enemy.BeHurt(currDamage,true);
        //血矛次数增加
        GameManager.Instance.bloodNum+=1;

        if(GameManager.Instance.haveSword)
        {
            Vector3 currPos = currAttackEnemy.transform.position;
            //生成特效
            GameObject newEff = Instantiate(finalSwordEff);
            newEff.transform.position = new Vector3(currPos.x, currPos.y + 0.5f, 0);
        }
    }
    
    //进入战斗
    public void EnterBattle(Player player,Enemy enemy)
    {
        GameManager.Instance.isBattle=true;
        currAttackEnemy = enemy;
        //保存一下数据
        SaveManager.instance.SaveAllData();
        //可以免疫
        GameManager.Instance.canHaveHurtEnd=true;
        //更新血矛次数
        GameManager.Instance.bloodNum=1;
        //更新骨棒次数
        skeletonNum=0;
        //更新连击矛数据
        moreSpearNum = 0;
        //临时攻击数据
        player.currOriArmor = player.armor;
        player.currOriDamage = player.damage;
        //如果拥有盾之心
        if (ArtifactManager.instance.SeekArtifact("盾之心"))
        {
            int addnum = Mathf.RoundToInt((enemy.data.armor*0.5f));
            player.armor+= addnum<1 ? 1 : addnum;
        }
        //如果拥有速血刀
        if(ArtifactManager.instance.SeekArtifact("速刃"))
        {
            //吸取敌人10%攻击
            int currAmount = Mathf.RoundToInt(enemy.data.damage*0.1f);
            currAmount=currAmount<1 ? 1 : currAmount;
            player.damage+=currAmount;
        }
        //如果是血月，且拥有月星
        if(GameManager.Instance.isBloodMoon&&ArtifactManager.instance.SeekArtifact("月星"))
        {
            int addnum = Mathf.RoundToInt((player.armor*0.5f));
            player.armor+=addnum<1 ? 1 : addnum;
            player.currOriDamage=player.damage;
            addnum = Mathf.RoundToInt((player.damage*0.5f));
            player.damage+=addnum<1 ? 1 : addnum;
        }
        //如果有羊符咒
        if (TurnManager.instance.SeekTurn("羊"))
        {
            int index = Mathf.RoundToInt(player.damage*(player.sheepStep/100.0f));
            player.damage+=index<=0 ? 0 : index;
        }
        //如果有狗符咒
        if (TurnManager.instance.SeekTurn("狗"))
        {
            int index = Mathf.RoundToInt(player.armor*(player.dogStep/100.0f));
            player.armor+=index<=0 ? 0 : index;
        }
        //清空羊狗步数
        player.sheepStep=0;
        player.dogStep=0;
        MsgStepUI msg = new MsgStepUI();
        msg.str="羊";
        EventManager.AddMsg(msg);
        msg.str="狗";
        EventManager.AddMsg(msg);
        //更新基础UI数据
        GameManager.Instance.UpdateMathData();
    }
    //退出战斗
    public void ExitBattle(Player player,Enemy enemy)
    {
        GameManager.Instance.isBattle=false;
        currAttackEnemy = null;
        //如果拥有盾之心
        if (ArtifactManager.instance.SeekArtifact("盾之心"))
        {
            player.armor=player.currOriArmor;
        }
        //如果是血月，且拥有月星
        if (GameManager.Instance.isBloodMoon&&ArtifactManager.instance.SeekArtifact("月星"))
        {
            player.damage=player.currOriDamage;
            player.armor=player.currOriArmor;
        }
        //如果拥有速血刀
        if (ArtifactManager.instance.SeekArtifact("速刃"))
        {
            player.damage=player.currOriDamage;
        }
        //如果有羊符咒
        if (TurnManager.instance.SeekTurn("羊"))
        {
            player.damage = player.currOriDamage;
        }
        //如果有狗符咒
        if (TurnManager.instance.SeekTurn("狗"))
        {
            player.armor = player.currOriArmor;
        }
        //更新基础UI数据
        GameManager.Instance.UpdateMathData();
    }
}
